#pragma once

#include "../Locker/Locker.h"

#include <string>
#include <mysql/mysql.h>
#include <list>

class DBConnectionPool
{
public:
    MYSQL* getConnection();
    bool releaseConnection();
    int freeConn(); // free number
    void destoryPool();

    // singleton design pattern
    static DBConnectionPool *getInstance();

    void init(std::string url, std::string user, std::string pwd, std::string dbName, int port, int maxConn, int closeLog);
private:
    DBConnectionPool();
    ~DBConnectionPool();

    using ConnList = std::list<MYSQL*>;

    int maxConn_;
    int curConn_; //number of connections that are currently in use
    int freeConn_;

    Locker lock_;
    Sem reserve_;

    ConnList connList_;

    std::string url_;   // host address
    std::string port_;  // database port
    std::string user_;  // username
    std::string pwd_;   // password
    std::string dbName_; 
    int closeLog_;
};

// @TODO: think about use smarter pointer instead
class DBConnectionRAII
{

};